package com.kuang.web.admin;

import com.kuang.pojo.Person;
import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class UploadController {
    @Autowired
    private UserService userService;

    @RequestMapping("/toupload")
    public String toupload() {
        return "admin/upload";
    }

    @RequestMapping("/upload")
//    @ResponseBody
    public String upload(MultipartFile file, User user, Model model, HttpSession session) throws IOException {
//        System.out.println(file);
//        System.out.println("user = " + user);
//        System.out.println("file = " + file);
//        String originalFilename = file.getOriginalFilename();
//        System.out.println("originalFilename = " + originalFilename);
        String filename = null;
        if (file != null) {
            if (!file.getOriginalFilename().equals("")) {
                String exString = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                filename = UUID.randomUUID().toString() + exString;
                InputStream in = file.getInputStream();
                FileOutputStream out = new FileOutputStream("C:\\upload01\\" + filename);
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = in.read(bytes)) != -1) {
                    out.write(bytes, 0, len);
                }
            }

        }

        user.setAvatar(filename); // 将图片的逻辑名称存在对象中
        user.setUpdateTime(new Date());
        user.setId(1);
//        System.out.println(user);
        int i = userService.updateUser(user);  // 修改用户
        System.out.println("i = " + i);
//        System.out.println(user);
        User user1 = userService.selectUser();  // 因为在配置页我们是用 登录时的存放在session的user对象 修改后session没更新
        session.removeAttribute("user");
        session.setAttribute("user", user1);      //  那么数据回显就还是之前的！
        return "redirect:/admin/toconfig";
    }

    @RequestMapping("/uploadavatar")
//    @ResponseBody
    public String uploadavatar(MultipartFile file) throws IOException {
        System.out.println("file = " + file);
        String filename = null;
        if (file != null) {
            if (!file.getOriginalFilename().equals("")) {
                String exString = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                filename = UUID.randomUUID().toString() + exString;
                InputStream in = file.getInputStream();
                FileOutputStream out = new FileOutputStream("C:\\upload02\\" + filename);
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = in.read(bytes)) != -1) {
                    out.write(bytes, 0, len);
                }
            }

        }
        return "redirect:/admin/toconfig";

    }

}
